package com.mimiwang.article.controller;

import com.mimiwang.article.entity.ArticleInfoEntity;
import com.mimiwang.article.service.ArticleInfoService;
import com.mimiwang.common.exception.BizCodeEnum;
import com.mimiwang.common.utils.PageUtils;
import com.mimiwang.common.utils.R;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Arrays;
import java.util.Map;




/**
 * 
 *
 * @author huangqicai
 * @email 1355518552@qq.com
 * @date 2022-10-07 15:31:26
 */
@Slf4j
@RestController
@RequestMapping("member/articleinfo")
public class ArticleInfoController {
    @Autowired
    private ArticleInfoService articleInfoService;

    /*主页显示*/

    /*点击写文章按钮执行这个方法*/
    /*判断是否登录*/
    @RequestMapping("/write")
    public R writeArticle(HttpServletRequest request){
        /*获取token字符串*/
        String s = articleInfoService.writeArticle(request);
        if(s==null){
            return R.error(BizCodeEnum.NOT_LOGGED_NO);
        }
        return R.ok();
    }

    /**
     * 点击文章正式发布就执行这个接口
     * categoryIds
     * articleInfo
     * imgUrls
     */
    @RequestMapping("/publish")
    public R publish(@RequestBody Map<String,Object> map, HttpServletRequest request){
        return articleInfoService.publish(map,request);
    }

    /*点击文章暂时保存，不发布就执行这个接口
    * categoryIds
    * articleInfo
    * imgUrls
    * */
    @RequestMapping("/save")
    public R saveArticle(@RequestBody Map<String,Object> map,HttpServletRequest request){
        return articleInfoService.saveArticle(map,request);
    }

    /*根据文章id进行删除*/
    @RequestMapping("/deleteById")
    public R deleteArticle(@RequestParam("articleId") Integer articleId,HttpServletRequest request){
        return articleInfoService.deleteArticle(articleId,request);
    }

    /*点击编辑按钮，进行数据回显*/
    @RequestMapping("/updateById/{articleId}")
    public R updateArticle(@PathVariable("articleId") Integer articleId,HttpServletRequest request){
        return articleInfoService.updateArticle(articleId,request);
    }


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = articleInfoService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{articleId}")
    public R info(@PathVariable("articleId") Long articleId){
		ArticleInfoEntity articleInfo = articleInfoService.getById(articleId);

        return R.ok().put("articleInfo", articleInfo);
    }



    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody ArticleInfoEntity articleInfo){
		articleInfoService.updateById(articleInfo);
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Long[] articleIds){
		articleInfoService.removeByIds(Arrays.asList(articleIds));

        return R.ok();
    }

}
